/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-07-06 20:57:49
 * @LastEditTime: 2024-09-04 21:46:50
 * @FilePath: /nest_study/server/src/common/filters/base-exception.filter.ts
 */
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { getNowDate } from "../utils/index";
import { Observable } from "rxjs";

//对post put请求数据进行基本信息处理
@Injectable()
export class BaseExceptionsFilter implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request: Request = context.switchToHttp().getRequest();
    const headers = request.headers;
    const method = request.method;
    let body = request.body;
    let userInfo = request['user'];
    if(userInfo){
      let username = userInfo.user.userName;
      if (method == 'POST') {
        body['createBy'] = username;
      }
      if (method == 'PUT') {
        body['updateTime'] = getNowDate();
        body['updateBy'] = username;
      }
    }
    // 然后继续传递修改后的参数
    return next.handle();
  }
}